/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.CartDao;
import gemini.basic.dao.GmnDao;
import gemini.basic.model.AbstractDomain;
import gemini.basic.model.Cart;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class CartDaoImpl extends HibernateDaoSupport implements CartDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public Cart saveOrUpdate(Cart cart, boolean flush) {
		Cart result = null;
		if (flush) {
			result = (Cart) gmnDao.saveOrUpdateAndFlush(cart);
		} else {
			result = (Cart) gmnDao.saveOrUpdate(cart);
		}
		return result;
	}
	
	@Override
	public Cart getById(int id) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Cart.class);

		if (id != AbstractDomain.TRANSIENT_ENTITY_ID) {
			dtCri.add(Restrictions.eq("id", id));
		}

		List<Object> resultList = gmnDao.findByCriteria(dtCri);

		Cart result = null;
		
		if (resultList != null && !resultList.isEmpty()) {
			result = (Cart) resultList.get(0);
		}
		return result;
	}

	@Override
	public Cart getOpenCartByDistributorId(int distributorId) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Cart.class);
		dtCri.createAlias("distributor","ds");
		dtCri.createAlias("cartStatuses","stt");
		
		dtCri.add(Restrictions.eq("stt.code", "INI"));
		dtCri.add(Restrictions.isNull("stt.validTo"));
		
		if (distributorId != AbstractDomain.TRANSIENT_ENTITY_ID) {
			dtCri.add(Restrictions.eq("ds.id", distributorId));
		}
		
		List<Object> cartList = gmnDao.findByCriteria(dtCri);

		Cart result = null;
		
		if (cartList != null && !cartList.isEmpty()) {
			result = (Cart) cartList.get(0);
		}
		return result;
	}

	@Override
	public List<Cart> getAllCart() {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Cart.class);

		List<Object> cartList = gmnDao.findByCriteria(dtCri);
		
		List<Cart> result = null;

		if (cartList != null && !cartList.isEmpty()) {
			result = new ArrayList<Cart>(cartList.size());
			Cart cart = null;
			for (Object ob : cartList) {
				cart = (Cart)ob;
				result.add(cart);
			}
		}
		
		return result;
	}

	@Override
	public boolean deleteCart(Cart cart) {
			getHibernateTemplate().delete(cart);
			return true;
	}

	@Override
	public List<Cart> findFinishedCarts() {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Cart.class);
		dtCri.createAlias("cartStatuses","stt");
		
		dtCri.add(Restrictions.eq("stt.code", "FIN"));
		dtCri.add(Restrictions.isNull("stt.validTo"));
		
		dtCri.add(Restrictions.isNotNull("payment"));
		
		
		List<Object> cartList = gmnDao.findByCriteria(dtCri);

		List<Cart> result = null;
		
		if (cartList != null) {
			result = new ArrayList<Cart>(cartList.size());
			for (Object cart : cartList) {
				result.add((Cart)cart);
			}
		}
		
		return result;
	}

	@Override
	public List<Cart> findUnusedCarts() {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Cart.class);
		dtCri.createAlias("cartStatuses","stt");
		
		dtCri.add(Restrictions.eq("stt.code", "UNU"));
		dtCri.add(Restrictions.isNull("stt.validTo"));
		
		List<Object> cartList = gmnDao.findByCriteria(dtCri);

		List<Cart> result = null;
		
		if (cartList != null) {
			result = new ArrayList<Cart>(cartList.size());
			for (Object cart : cartList) {
				result.add((Cart)cart);
			}
		}
		
		return result;
	}

}
